#! /usr/bin/python
# -*- coding: cp1250 -*-

import re
import string
import sys
import os
import time
import pepe.p_txt.bigtxt as bt

class TxtRaw:
    ''' 
    Text file processing - keep, drop lines

    Example:
    import pepe.p_utils.txtr as tr
    
    f = '000.D001.Dodavatele.txt'
    c = tr.TxtRaw()
    c.ifile = f
    #c.header = 'a^b^c'
    c.keep = ['^\d ', '^P ']
    c.run('001.' + f[4:])

    OR

    c = tr.TxtRaw()
    for f in glob.glob('*.dat'):
        c.ifile = f
        #c.header = 'a^b^c'
        c.keep = ['^\d ', '^P ']
        c.run('001.' + f[4:])
    '''
    def __init__(self):
        ''' '''
        self.ifile = ''
        self.keep = ['']
        self.drop = ['']
        self.header = ''

    def run(self, ofile):
        ''' '''
        print "\tNote: Output", ofile, "is APPENDED, not re-written!\n"
        of = open(ofile, 'a')

        if self.header <> '': of.write(self.header)
        for L in bt.lines(self.ifile):
            write = False
            for i in self.keep:
                if write == False:
                    m = re.search(i, L)
                    if m: write = True
            for i in self.drop:
                if write == True and self.drop <> ['']:
                    m = re.search(i, L)
                    if m: write = False
            if write: of.write(L)
        of.close()

if __name__ == "__main__":
    ''' '''
    print re.sub("\n    ", "\n", TxtRaw.__doc__)

    if len(sys.argv) == 1:
        pass
    else:
        pass
